using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Pegasus.Runtime.Serialization.Formatters.Xml;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	[TestClass]
	public class XmlFormatter2RegressTestWorkflowContext
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref='T:XmlFormatter2RetoTestGenericList'/> class.
		/// </summary>
		public XmlFormatter2RegressTestWorkflowContext()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Serializes the object references and return the object via a proxy.
		/// </summary>
		[TestMethod]
		public void WorkflowRegressionTest1()
		{
			object obj = null;

			// Deserialize the buffer to an object
			byte[] buffer = Encoding.UTF8.GetBytes( m_XmlTest1 );
			using( MemoryStream memStream = new MemoryStream( buffer ) )
			{
				XmlFormatter2 xmlFormatter = new XmlFormatter2( null, new StreamingContext( StreamingContextStates.Persistence ) );
				obj = xmlFormatter.Deserialize( memStream );
			}
		}

		/// <summary>
		/// Serializes the object references and return the object via a proxy.
		/// </summary>
		[TestMethod]
		public void WorkflowRegressionTest2()
		{
			object obj = null;

			// Deserialize the buffer to an object
			byte[] buffer = Encoding.UTF8.GetBytes( m_XmlTest2 );
			using( MemoryStream memStream = new MemoryStream( buffer ) )
			{
				XmlFormatter2 xmlFormatter = new XmlFormatter2( null, new StreamingContext( StreamingContextStates.Persistence ) );
				obj = xmlFormatter.Deserialize( memStream );
			}
		}

		// This is the XML that broke
		private string m_XmlTest1 = @"
			<XmlFormatter Version='2.0'>
			  <ObjectGraph TypeRef='4' ObjectId='1' ISerializable='true'>
				  <_items TypeRef='5'>
					<_items_element TypeRef='6' ObjectId='3'>
					  <BindingType TypeRef='7' ObjectId='4'>
						<value__ TypeRef='3'>2</value__>
					  </BindingType>
					  <WorkflowId TypeRef='3'>775</WorkflowId>
					  <StateName TypeRef='2'>PrintingState</StateName>
					  <MethodName TypeRef='2'>DataExchange_PrintingComplete</MethodName>
					</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
				  </_items>
				  <_size TypeRef='3'>1</_size>
				  <_version TypeRef='3'>1</_version>
			  </ObjectGraph>
			  <TypeReferences>
				<Type TypeRef='1'>Pegasus.Workflow.Service.WorkflowContext, Pegasus.Library, Version=2.0.192.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='2'>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='3'>System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='4'>System.Collections.Generic.List`1[[Pegasus.Workflow.Service.WorkflowContext+WorkflowCompletedEventInfo, Pegasus.Library, Version=2.0.192.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='5'>Pegasus.Workflow.Service.WorkflowContext+WorkflowCompletedEventInfo[], Pegasus.Library, Version=2.0.192.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='6'>Pegasus.Workflow.Service.WorkflowContext+WorkflowCompletedEventInfo, Pegasus.Library, Version=2.0.192.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='7'>Pegasus.Workflow.Service.WorkflowContext+WorkflowCompletedBindingType, Pegasus.Library, Version=2.0.192.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='8'>System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
			  </TypeReferences>
			</XmlFormatter>		
		";

		private string m_XmlTest2 = @"
			<XmlFormatter Version='2.0'>
			  <ObjectGraph TypeRef='4' ObjectId='1'>
				  <_items TypeRef='5'>
					<_items_element TypeRef='6' ObjectId='3'>
					  <BindingType TypeRef='7' ObjectId='4'>
						<value__ TypeRef='3'>2</value__>
					  </BindingType>
					  <WorkflowId TypeRef='3'>4732</WorkflowId>
					  <StateName TypeRef='2'>PrintingState</StateName>
					  <MethodName TypeRef='2'>DataExchange_PrintingComplete</MethodName>
					</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
					<_items_element TypeRef='8'>null</_items_element>
				  </_items>
				  <_size TypeRef='3'>1</_size>
				  <_version TypeRef='3'>1</_version>
			  </ObjectGraph>
			  <TypeReferences>
				<Type TypeRef='1'>Pegasus.Workflow.Service.WorkflowContext, Pegasus.Library, Version=2.0.274.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='2'>System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='3'>System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='4'>System.Collections.Generic.List`1[[Pegasus.Workflow.Service.WorkflowEventInfo, Pegasus.Library, Version=2.0.274.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
				<Type TypeRef='5'>Pegasus.Workflow.Service.WorkflowEventInfo[], Pegasus.Library, Version=2.0.274.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='6'>Pegasus.Workflow.Service.WorkflowEventInfo, Pegasus.Library, Version=2.0.274.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='7'>Pegasus.Workflow.Service.WorkflowEventType, Pegasus.Library, Version=2.0.274.0, Culture=neutral, PublicKeyToken=a7ad7231644e419b</Type>
				<Type TypeRef='8'>System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</Type>
			  </TypeReferences>
			</XmlFormatter>";

	}
}
